package com.xxl.sodmall.mvp.presenter

import android.text.TextUtils
import com.dgshanger.sanhxiaofeisc.mvp.contract.ForgetPwContract
import com.dgshanger.sanhxiaofeisc.mvp.model.ForgetPwModel
import com.xx.baseuilibrary.mvp.BaseMvpPresenter
import com.xx.baseutilslibrary.network.rx.XxBaseHttpObserver
import io.reactivex.Observable
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.observers.DefaultObserver
import java.util.concurrent.TimeUnit

/**
 * ForgetPwPresenter
 * 沉迷学习不能自拔
 * Describe：
 * Created by 雷小星🍀 on 2018/2/6 11:42.
 */

class ForgetPwPresenter : BaseMvpPresenter<ForgetPwContract.Model, ForgetPwContract.View>(), ForgetPwContract.Presenter {
    override fun createModel(): ForgetPwContract.Model = ForgetPwModel()

    override fun changePw() {
        if (getView() == null)
            return
        val phone = getView()!!.phone
        val vCode = getView()!!.vCode
        val newPw = getView()!!.newPw
        val surePw = getView()!!.surePw
        if (TextUtils.isEmpty(phone) || phone.length != 11) {
            getView()?.showToast("请输入正确的手机号")
            return
        }
        if (TextUtils.isEmpty(newPw) || newPw.length < 6) {
            getView()?.showToast("请输入密码,至少6位")
            return
        }
        if (TextUtils.isEmpty(surePw) || surePw.length < 6) {
            getView()?.showToast("请确认密码,至少6位")
            return
        }
        if (newPw != surePw) {
            getView()?.showToast("两次输入密码不一致")
            return
        }
        if (TextUtils.isEmpty(vCode) || vCode.length != 6) {
            getView()?.showToast("请输入完整的验证码")
            return
        }
        getModel().changePw(phone, vCode, newPw, object : XxBaseHttpObserver<Any>() {
            override fun onCompleted(msg: String?, entity: Any?) {
                getView()?.showToast(msg)
                getView()?.changeSuccess()
            }

            override fun onError(error: String?) {
                getView()?.showToast(error)
            }

            override fun onStart() {
                isLoading = true
                getView()?.showLoadingDialog()
            }

            override fun onFinish() {
                isLoading = false
                getView()?.dismissLoadingDialog()
            }
        })
    }

    override fun sendVCode() {
        val phone = getView()?.phone!!
        if (TextUtils.isEmpty(phone) || phone.length != 11) {
            getView()?.showToast("请输入正确的手机号")
            return
        }
        getModel().sendVCode(phone, object : XxBaseHttpObserver<Any>() {
            override fun onCompleted(msg: String?, entity: Any?) {
                getView()?.showToast(msg)
                //开始倒计时
                val timeCount = 60
                Observable.interval(0, 1, TimeUnit.SECONDS)
                        .take((timeCount + 1).toLong())
                        .map { aLong -> timeCount - aLong!! }
                        .doOnSubscribe {
                            //发送数据中,将发送按钮状态设置为不可用
                            getView()?.setSendBtnEnable(false)

                        }
                        .observeOn(AndroidSchedulers.mainThread())
                        .subscribe(object : DefaultObserver<Long>() {
                            override fun onNext(value: Long?) {
                                getView()?.setSendBtnText("(" + value + "s)")
                            }

                            override fun onError(e: Throwable) {
                                getView()?.setSendBtnEnable(true)
                                getView()?.setSendBtnText("重试")
                            }

                            override fun onComplete() {
                                getView()?.setSendBtnEnable(true)
                                getView()?.setSendBtnText("重新发送")
                            }
                        })
            }

            override fun onError(error: String?) {
                getView()?.showToast(error)
            }

            override fun onStart() {
                isLoading = true
                getView()?.showLoadingDialog("验证码发送中")
            }

            override fun onFinish() {
                isLoading = false
                getView()?.dismissLoadingDialog()
            }
        })
    }
}
